package com.whut.transportation.overweight;

import java.util.HashMap;

import com.whut.transportation.LoginActivity;
import com.whut.transportation.PersonalPasswordActivity;
import com.whut.transportation.R;
import com.whut.transportation.entity.Message;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.ui.BaseActivity.DataCallback;
import com.whut.transportation.utils.AppManager;
import com.whut.transportation.utils.Constant;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class OWPersonalPasswordActivity extends BaseActivity{
	private EditText oldpassword;
	private EditText newpassword;
	private EditText comfirmpassword;
	
	private Button submit_password;
	
	private SharedPreferences preferences;

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		oldpassword = (EditText) findViewById(R.id.oldpassword);
		newpassword = (EditText) findViewById(R.id.newpassword);
		comfirmpassword = (EditText) findViewById(R.id.comfirmpassword);
		submit_password = (Button) findViewById(R.id.submit_password);
	}

	@Override
	protected void afterFindViewId() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void loadViewLayout() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_ow_personal_password);
		setTitleBarView(true, "修改密码", -1, true);
		preferences=getSharedPreferences("userId", Context.MODE_PRIVATE);//获取偏好数据
	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub
		String id = preferences.getString("id", null);
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		submit_password.setOnClickListener(this);
	}

	@Override
	protected void getDataAgain() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit_password:
			//提交按钮提交执行密码更新操作
			updatePassword();
			break;
		default:
			break;
		}
		super.onClick(v);
	}

	private void updatePassword() {
		// TODO Auto-generated method stub
		String oldPassword = oldpassword.getText().toString().trim();
		String newPassword = newpassword.getText().toString().trim();
		String comfirmPassword = comfirmpassword.getText().toString().trim();
		
		if(oldPassword.length()>0&&newPassword.length()>0&&newPassword.length()>0){
			if(!newPassword.equals(comfirmPassword)){
				Toast.makeText(mContext, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
			} else {
				//初始化更新请求参数
				HttpRequestVo vo = new HttpRequestVo();
				vo.requestUrl = Constant.UPDATE_PASSWORD;
				vo.context = mContext;
				
				HashMap<String, String> req = new HashMap<String, String>();
				req.put("account", preferences.getString("account", null));
				req.put("oldpassword", oldPassword);
				req.put("newpassword", newPassword);
				vo.requestDataMap = req;
				//返回数据解析的类为Message
				vo.parser = new JSONParser(Message.class);
				//从服务器中取回数据
				getDataFromServer(HttpManager.POST_MOTHOD, vo, 
						new DataCallback<HashMap<String, Object>>() {
							@Override
							public void onStart() {
							}
							@Override
							public void onFailed() {
								showNetErrorDialog();
							}
							@Override
							public void processData(
									HashMap<String, Object> paramObject,
									boolean paramBoolean) {
								Message message = (Message) paramObject.get("result");
								//返回数据的信息为1时 表示输入的密码有误，2表示更新出错，0表示修改成功
								if(message.getMessage() == 1)
									Toast.makeText(mContext, "您输入的密码有误", Toast.LENGTH_SHORT).show();
								else if(message.getMessage() == 2){
									Toast.makeText(mContext, "密码修改失败，您需要重新登陆", Toast.LENGTH_LONG).show();
									AppManager.getAppManager().finishAllActivity();
									Intent intent = new Intent(OWPersonalPasswordActivity.this,LoginActivity.class);
									startActivity(intent);
								} else if(message.getMessage() == 0){
									Toast.makeText(mContext, "修改成功", Toast.LENGTH_SHORT).show();
									AppManager.getAppManager().finishAllActivity();
									Intent intent = new Intent(OWPersonalPasswordActivity.this,LoginActivity.class);
									startActivity(intent);
								}
							}
							@Override
							public void onFinish() {
								closeProgressDialog();
							}
						});
			}
		} else {
			Toast.makeText(mContext, "密码不能为空", Toast.LENGTH_SHORT).show();
		}
		
	}
}
